package com.jp.knowledge.activity;


import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.Wallet;
import com.jp.knowledge.util.IPUtil;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

/**
 * @author jiayen
 *
 * 提现页面
 */
public class WithdrawmoneyActivity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

    /***提现*/
    private final int WITHDRAW_AMOUNT=0x0000000001;

    /**确定按钮*/
    @ViewInject(R.id.pay_btn)
    private TextView payBtn;

    /***金额*/
    @ViewInject(R.id.amount)
    private TextView amount;

    /**提现金额**/
    @ViewInject(R.id.amount_get)
    private TextView  amountEdit;

    private Wallet  wallet;
    @Override
    protected int getResourceId() {
        return R.layout.activity_withdrawmoney;
    }

    @Override
    protected void init() {
        wallet = (Wallet) getIntent().getSerializableExtra("wallet");

        topName.setText("提现");
        rightBtn.setText("历史");
        rightBtn.setVisibility(View.VISIBLE);
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        leftIcon.setOnClickListener(this);

        payBtn.setOnClickListener(this);

        if(wallet!=null){
            float money = wallet.getTotalAmount()/100f;
            amount.setText(money+"");
        }

    }

    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case  R.id.icon_left:
                    this.finish();
                    break;

                case  R.id.pay_btn://执行提现
                    handWithDraw();
                    break;
                default:
                    break;

            }
    }

    /**
     * 执行提现
     */
    private void handWithDraw() {
        JsonObject param = new JsonObject();

        String amountStr =amountEdit.getText().toString();
        if(!TextUtils.isEmpty(amountStr)){
            int amount = (int) (Float.valueOf(amountStr)*100);
            if(amount<1000){
                ToasUtil.toast(mContext,"最少提现10元!");
                return;
            }
            param.addProperty("amount",amount);
            param.addProperty("client_ip", IPUtil.getLocalIPAddress());
            HttpHelper.getInstance(mContext).withDrawAmount(param,WITHDRAW_AMOUNT,this);
        }else {
           ToasUtil.toast(mContext,"请输入金额");
        }

    }

    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {

    }

    @Override
    public void onError(int requestType) {

    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code =data.getErrcode();
        if(requestType==WITHDRAW_AMOUNT){
            if(code==0){
                ToasUtil.toast(mContext,"操作成功，请尽快去微信领取");
            }else{
                ToasUtil.toast(mContext,data.getMessage());
            }
        }
    }
}
